package cn.signalpha.modules.shop.controller.seller;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.common.constant.CommonConstant;
import cn.signalpha.common.constant.MsgConstant;
import cn.signalpha.common.utils.Assert;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.secure.TokenUser;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.*;
import cn.signalpha.modules.shop.dto.SellerAccountDTO;
import cn.signalpha.modules.shop.entity.Seller;
import cn.signalpha.modules.shop.entity.enums.SellerAccountTypeEnum;
import cn.signalpha.modules.shop.service.SellerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Date;
import java.util.Map;
import java.util.Objects;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SHOP_NAME + "/sys-seller/seller/account")
@Api(value = "商户子账号", tags = "商户子账号")
public class SpiSellerAccountController extends BaseController {

	private final SellerService sellerService;

	@GetMapping("/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name="name", dataType = "string", value = "名称"),
	})
	@ApiOperation(value = "分页", notes = "")
	public R<IPage<Seller>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query, TokenUser tokenUser) {
		QueryWrapper<Seller> wrapper = sellerService.selectSellerWrapper(params);
		wrapper.lambda().eq(Seller::getSellerId, tokenUser.getTenantId());
		wrapper.lambda().eq(Seller::getAccountType, SellerAccountTypeEnum.MINOR.getType());
		return R.data(sellerService.page(Condition.getPage(query), wrapper));
	}

	@GetMapping("/info")
	@ApiOperation(value = "详情", notes = "传入id")
	public R<Seller> info(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
		return R.data(sellerService.getById(id));
	}

	@PostMapping("/save")
	@ApiOperation(value = "新增", notes = "")
	public R save(@Valid @RequestBody SellerAccountDTO sellerAccountDTO, TokenUser tokenUser) {
		long count = sellerService.count(Wrappers.<Seller>lambdaQuery().eq(Seller::getAccount, sellerAccountDTO.getAccount()));
		Assert.isTrue(count == 0, StringUtil.format("账号[{}]已被使用", sellerAccountDTO.getAccount()));
		Seller seller = Objects.requireNonNull(BeanUtil.copy(sellerAccountDTO, Seller.class));
		seller.setSellerId(Long.valueOf(tokenUser.getTenantId()));
		seller.setAccountType(SellerAccountTypeEnum.MINOR.getType());
		seller.setStatus(1);
		seller.setCreateTime(new Date());
		seller.setUpdateTime(new Date());
		seller.setPassword(DigestUtil.encrypt(sellerAccountDTO.getPassword()));
		return R.status(sellerService.save(seller));
	}

	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "传入store")
	public R update(@Valid @RequestBody SellerAccountDTO sellerAccountDTO) {
		Seller seller = sellerService.getById(sellerAccountDTO.getId());
		Assert.isNull(seller, MsgConstant.OBJECT_NULL);
		if(!seller.getAccount().equals(sellerAccountDTO.getAccount())){
			long count = sellerService.count(Wrappers.<Seller>lambdaQuery().eq(Seller::getAccount, sellerAccountDTO.getAccount()));
			Assert.isTrue(count == 0, StringUtil.format("账号[{}]已被使用", sellerAccountDTO.getAccount()));
		}

		sellerAccountDTO.setPassword(null);
		sellerAccountDTO.setUpdateTime(new Date());
		return R.status(sellerService.updateById(BeanUtil.copy(sellerAccountDTO, Seller.class)));
	}

	@PostMapping("/remove")
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(sellerService.removeByIds(Func.toLongList(ids)));
	}

	@PostMapping("/reset/password")
	@ApiOperation(value = "重置密码", notes = "重置为123456")
	public R resetPassword(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		Seller store = Seller.builder().password(DigestUtil.encrypt(CommonConstant.DEFAULT_PASSWORD)).updateTime(DateUtil.now()).build();
		return R.status(sellerService.update(store, Wrappers.<Seller>lambdaUpdate().in(Seller::getId, Func.toLongList(ids))));
	}

}
